package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.TaskComplete;
import com.company.project.service.TaskCompleteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by CodeGenerator on 2020/04/21.
*/
@RestController
@RequestMapping("/task/complete")
public class TaskCompleteController {
    @Resource
    private TaskCompleteService taskCompleteService;

    @GetMapping("/page/manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/taskComplete/taskCompleteList.html");
        return modelAndView;
    }

    @GetMapping("/page/detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/taskComplete/taskCompleteAdd.html");
        return modelAndView;
    }

    @PostMapping("/add")
    public Result add(TaskComplete taskComplete) {
        taskCompleteService.save(taskComplete);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        taskCompleteService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(TaskComplete taskComplete) {
        taskCompleteService.update(taskComplete);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        TaskComplete taskComplete = taskCompleteService.findById(id);
        return ResultGenerator.genSuccessResult(taskComplete);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TaskComplete> list = taskCompleteService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
